package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_logerp;

import cn.com.api.service.ApiT_logerpService;

import cn.com.util.*;
import cn.com.util.*;

public class T_logerpBaseController {

	private static Logger logger = Logger
			.getLogger(T_logerpBaseController.class);

	@Autowired
	private ApiT_logerpService apiT_logerpService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_logerp-list")
	public ModelAndView t_logerp_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_logerp_list.jsp");

		List<ApiT_logerp> t_logerps = apiT_logerpService.findT_logerps();
		mv.addObject("t_logerps", t_logerps);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_logerp-add-pre")
	public ModelAndView t_logerp_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_logerp_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_logerp t_logerp = apiT_logerpService.findT_logerpById(Integer
					.valueOf(id));
			mv.addObject("t_logerp", t_logerp);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_logerp-add")
	public void t_logerp_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String type_id = request.getParameter("type_id");
		String product_id = request.getParameter("product_id");
		String goods_id = request.getParameter("goods_id");
		String create_time = request.getParameter("create_time");
		String update_time = request.getParameter("update_time");
		String ip = request.getParameter("ip");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_logerp t_logerp = new ApiT_logerp();
			/*
			 * t_logerp.setId(id); t_logerp.setType_id(type_id);
			 * t_logerp.setProduct_id(product_id);
			 * t_logerp.setGoods_id(goods_id);
			 * t_logerp.setCreate_time(create_time);
			 * t_logerp.setUpdate_time(update_time); t_logerp.setIp(ip);
			 */
			apiT_logerpService.insertT_logerp(t_logerp);
		}
		// edit
		else {
			ApiT_logerp t_logerp = apiT_logerpService.findT_logerpById(Integer
					.valueOf(id));
			/*
			 * t_logerp.setId(id); t_logerp.setType_id(type_id);
			 * t_logerp.setProduct_id(product_id);
			 * t_logerp.setGoods_id(goods_id);
			 * t_logerp.setCreate_time(create_time);
			 * t_logerp.setUpdate_time(update_time); t_logerp.setIp(ip);
			 */
			apiT_logerpService.updateT_logerp(t_logerp);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_logerp-delete")
	public void t_logerp_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_logerpService.deleteT_logerp(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
